--chcp 65001
-- 定义图形基类
local Shape = {}
Shape.__index = Shape

-- 图形基类构造函�?
function Shape:new(width, height)
    local instance = {}
    instance.width = width or 0
    instance.height = height or 0
    instance.area = 0
    instance.perimeter = 0
    setmetatable(instance, self)
    return instance
end

-- 基类的计算周长方法（默认实现�?
function Shape:calculatePerimeter()
    self.perimeter = 0
    return self.perimeter
end
 
-- 基类的计算面积方法（默认实现�?
function Shape:calculateArea()
    self.area = 0
    return self.area
end

-- 定义三角形类
local Triangle = {}
Triangle.__index = Triangle
setmetatable(Triangle, {__index = Shape}) -- 继承自Shape

-- 三角形构造函�?
function Triangle:new(width, height)
    local instance = Shape:new(width, height)
    setmetatable(instance, self)
    return instance
end

-- 重写三角形计算周长方法（假设是等腰三角形�?
function Triangle:calculatePerimeter()
    -- 假设底边是width，其他两边相�?
    local side = math.sqrt((self.width/2)^2 + self.height^2)
    self.perimeter = self.width + 2 * side
    return self.perimeter
end

-- 重写三角形计算面积方�?
function Triangle:calculateArea()
    self.area = 0.5 * self.width * self.height
    return self.area
end

-- 定义圆形�?
local Circle = {}
Circle.__index = Circle
setmetatable(Circle, {__index = Shape}) -- 继承自Shape

-- 圆形构造函�?
function Circle:new(radius)
    local instance = Shape:new(radius, radius)
    instance.radius = radius
    setmetatable(instance, self)
    return instance
end

-- 重写圆形计算周长方法
function Circle:calculatePerimeter()
    self.perimeter = 2 * math.pi * self.radius
    return self.perimeter
end

-- 重写圆形计算面积方法
function Circle:calculateArea()
    self.area = math.pi * self.radius * self.radius
    return self.area
end

-- 定义矩形�?
local Rectangle = {}
Rectangle.__index = Rectangle
setmetatable(Rectangle, {__index = Shape}) -- 继承自Shape

-- 矩形构造函�?
function Rectangle:new(width, height)
    local instance = Shape:new(width, height)
    setmetatable(instance, self)
    return instance
end

-- 重写矩形计算周长方法
function Rectangle:calculatePerimeter()
    self.perimeter = 2 * (self.width + self.height)
    return self.perimeter
end

-- 重写矩形计算面积方法
function Rectangle:calculateArea()
    self.area = self.width * self.height
    return self.area
end

-- 定义正方形类
local Square = {}
Square.__index = Square
setmetatable(Square, {__index = Rectangle}) -- 继承自Rectangle

-- 正方形构造函�?
function Square:new(width)
    local instance = Rectangle:new(width, width)
    setmetatable(instance, self)
    return instance
end

-- 测试各个图形�?
print("===== 三角�? =====")
local triangle = Triangle:new(6, 8)
print("面积: " .. triangle:calculateArea())
print("周长: " .. triangle:calculatePerimeter())

print("\n===== 圆形 =====")
local circle = Circle:new(5)
print("圆形面积: " .. circle:calculateArea())
print("圆形周长: " .. circle:calculatePerimeter())

print("\n===== 矩形 =====")
local rectangle = Rectangle:new(4, 6)
print("矩形面积: " .. rectangle:calculateArea())
print("矩形周长: " .. rectangle:calculatePerimeter())

print("\n===== 正方�? =====")
local square = Square:new(5)
print("正方形面�?: " .. square:calculateArea())
print("正方形周�?: " .. square:calculatePerimeter())